import {
    EtherMonkeyKingClient,
    EtherArrtibuteFactoryClient,
    EtherArrtibuteClient
    // EtherERC721Client,
} from "../src/sdk/index";

import {
    ethers ,
    BigNumber,
    ContractReceipt,
    providers
}from "ethers";

import pino from 'pino';
const Logger = pino();
import {
    ethers as hardhat_ethers, 
    network
} from 'hardhat';

const mnemonic =  "artefact zone average skill hurt bonus permit polar gospel way left rifle";
// import HDWalletProvider = require("@truffle/hdwallet-provider");
export async function  GetFactory(address : string ) {
    let client = new EtherMonkeyKingClient();
    let factoryClient = new EtherArrtibuteFactoryClient();

    const provider = ethers.getDefaultProvider("rinkeby");

    const _wallet = ethers.Wallet.fromMnemonic(mnemonic);
    const wallet = _wallet.connect(provider);
    
    client.connectProvider(address,provider)
    client.connectSigner(wallet)

    await client.getArrtibutesFactory().then(
        (arrs) => {
            for(let i = 0; i<arrs.length;i++){
                console.log("factory " + i + " :" + arrs[i] );
            }
        }
    ).catch(
        (error) => {
            Logger.error(`the error is:+ ${error} `);
            process.exit(1);
        }
    );

    await client.name().then(
        (name)=>{
            console.log("代币名称:"+name);
        }
    )
    
}

export async function NewArrtibuteAssert(factoryAddress :string){
    let factoryClient = new EtherArrtibuteFactoryClient();

    const provider = ethers.getDefaultProvider("rinkeby");
    const _wallet = ethers.Wallet.fromMnemonic(mnemonic);
    const wallet = _wallet.connect(provider);

    factoryClient.connectProvider(factoryAddress,provider);
    factoryClient.connectSigner(wallet);

    await factoryClient.getTraitType().then(
        (t)=>{
            console.log("type:"+t);
        }
    ).catch((err) => {
        console.error(err);
        process.exit(1);
    })

    await factoryClient.NewArrtibuteAssert(
        "MonkeyKing_Index",
        "QM12345678",
        "QM123456789",
        10,
        `MonkeyKing`,
        `MKK`,
    ).then(
        (receipt) =>{
            receipt as ContractReceipt;
            console.log("RECEIPT:"+Object.entries(receipt));
        }
    ).catch((err) => {
        console.error(err);
        process.exit(1);
    })     

    let assert = await factoryClient.getAsserts()
    await MintArrtibute(assert[0]._assert);

} 


export async function MintArrtibute(assert_addr :string){
    let AssertClient = new EtherArrtibuteClient();

    const provider = ethers.getDefaultProvider("rinkeby");

    const _wallet = ethers.Wallet.fromMnemonic(mnemonic);
    const wallet = _wallet.connect(provider);

    AssertClient.connectProvider(assert_addr,provider);
    AssertClient.connectSigner(wallet);

    await AssertClient.name().then(
        (name)=>{
            console.log("name:"+name);
        })
    await AssertClient.getArtwork_image_uri().then(
        (uri)=>{
            console.log("image:"+uri);
        }
    )

    await AssertClient.safeMint(wallet.address).then(
        (receipt=>{
            console.log("receipt:"+receipt);
        })
    ).catch((error)=>{
        console.error(error);
        process.exit(1);
    })
    
}

GetMonkeyKingByTokenId("0xfeAbBebc5F294399e68a22b46Cfa8e0F762C4721");
NewArrtibuteAssert("0x5e8ccd4f7C7d1216Ce0ddea885c22225Ab7f86de");








